﻿using System;
using System.ComponentModel;
using System.IO;
using System.Timers;
using System.Windows;
using System.Windows.Threading;
using iStudio.Library;
using iStudio.Structure.Engine;
using iStudio.View;

namespace iStudio.Control.Audio
{
    /// <summary>
    /// Interaction logic for PlaybackControl.xaml
    /// </summary>
    public partial class PlaybackControl
    {
        private bool _isRewinding;
        private readonly Timer _dispatcherTimer = new Timer(10);

        public PlaybackControl()
        {
            InitializeComponent();

            //
            // Setup timer for rewinding & forwarding
            //

            _dispatcherTimer.Elapsed += DispatcherTimerTick;

            AudioEngine soundEngine = AudioEngine.Instance;
            soundEngine.PropertyChanged += AudioEnginePropertyChanged;
        }

        private void AudioEnginePropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            AudioEngine engine = AudioEngine.Instance;

            switch (e.PropertyName)
            {
                case "ChannelPosition":

                    if ( (engine.ChannelPosition >= engine.Project.Length || engine.ChannelPosition <= 0) && (!engine.IsPlaying) )
                    {
                        button_play.Content = "4";
                    }

                    break;
                case "IsPlaying":
                    button_play.Content = engine.IsPlaying ? ";" : "4";

                    break;
                case "IsRecording":
                    if (engine.IsRecording)
                    {
                        button_play.Content = "g";
                        button_play.FontSize = 12;
                    }
                    else
                    {
                        button_play.Content = "4";
                        button_play.FontSize = 28;
                    }

                    break;
            }
        }

        private void ButtonPlayClick(object sender, RoutedEventArgs e)
        {
            if (AudioEngine.Instance.IsPlaying)
            {
                if (AudioEngine.Instance.CanPause)
                {
                    AudioEngine.Instance.Pause();
                }
            }
            else if (AudioEngine.Instance.IsRecording)
            {
                if (AudioEngine.Instance.CanStop)
                {
                    AudioEngine.Instance.Stop();

                    MainWindow window = (MainWindow)Application.Current.MainWindow;
                    //window.RefreshUi ();
                    window.ConsoleLog ("Recorded sound: " + AudioEngine.Instance.ChannelLength + " seconds");
                }
            }
            else
            {
                if (AudioEngine.Instance.CanPlay)
                {
                    AudioEngine.Instance.Play();
                }
            }
        }

        private void ButtonStopClick(object sender, RoutedEventArgs e)
        {
            if (AudioEngine.Instance.CanStop)
            {
                AudioEngine.Instance.Stop();
                AudioEngine.Instance.SelectionBegin = new TimeSpan(0);
                AudioEngine.Instance.SelectionEnd = new TimeSpan(0);
                AudioEngine.Instance.ChannelPosition = 0.0;
            }
        }

        /*
        private void ButtonPauseClick (object sender, RoutedEventArgs e)
        {
            if (AudioEngine.Instance.CanPause)
            {
                AudioEngine.Instance.Pause ();
            }
        }*/

        private void ButtonRepeatClick(object sender, RoutedEventArgs e)
        {
            AudioEngine.Instance.Repeat = !AudioEngine.Instance.Repeat;
        }

        private void Rewind()
        {
            if (AudioEngine.Instance.IsPlaying)
            {
                if (_isRewinding == false)
                {
                    if ((AudioEngine.Instance.ChannelPosition + 0.05) < AudioEngine.Instance.ChannelLength)
                    {
                        AudioEngine.Instance.ChannelPosition += 0.05;
                    }
                    else
                    {
                        AudioEngine.Instance.ChannelPosition = AudioEngine.Instance.ChannelLength;
                    }
                }
                else
                {
                    if ((AudioEngine.Instance.ChannelPosition - 0.05) > 0.05)
                    {
                        AudioEngine.Instance.ChannelPosition -= 0.05;
                    }
                    else
                    {
                        AudioEngine.Instance.ChannelPosition = 0.0;
                    }
                }
            }
        }

        private void ButtonForwardMouseDown(object sender, RoutedEventArgs e)
        {
            _isRewinding = false;
            _dispatcherTimer.Start();
        }

        private void ButtonBackMouseDown(object sender, RoutedEventArgs e)
        {
            _isRewinding = true;
            _dispatcherTimer.Start();
        }

        private void ButtonForwardMouseUp(object sender, RoutedEventArgs e)
        {
            _isRewinding = false;
            _dispatcherTimer.Stop();
        }

        private void ButtonBackMouseUp(object sender, RoutedEventArgs e)
        {
            _isRewinding = true;
            _dispatcherTimer.Stop();
        }

        private void ButtonRecClick(object sender, RoutedEventArgs e)
        {
            AudioEngine.Instance.Record();
        }

        private void DispatcherTimerTick(object sender, ElapsedEventArgs elapsedEventArgs)
        {
            Dispatcher.Invoke(DispatcherPriority.Send, new Action(Rewind));
        }
    }
}
